﻿package game.account.shop {
	import flash.events.Event;
	import flash.events.MouseEvent;
	import game.ui.Button;
	import game.ui.DynamicClip;
	import game.objects.DynamicObject;
	import game.ui.Slider;
	
	public class ShopCar extends DynamicClip {
		
		private function get slider():Slider		{ return getChildByName('slider')		as Slider;	}
		private function get btnShopLeft():Button	{ return getChildByName('btnShopLeft')	as Button;	}
		private function get btnShopRight():Button	{ return getChildByName('btnShopRight')	as Button;	}
		
		public function ShopCar():void {
			var skin:DynamicObject = config.getSkin(DynamicObject.SHOP_CAR);
			
			super(skin);
			
			if (!skin) return;
			
			if (!slider || !btnShopLeft || !btnShopRight) return;
			
			btnShopLeft.addEventListener	( MouseEvent.CLICK, function ( event:Event )	{ step(-1);	});
			btnShopRight.addEventListener	( MouseEvent.CLICK, function ( event:Event )	{ step(1);	});
			
			addItem ( 1, 1000 );
			
		}
		
		public function addItem(id:int, cost:Number):void {
			var item:ShopCarItem = new ShopCarItem(id, cost);
			item.addEventListener(MouseEvent.CLICK, selectItem);
			slider.add ( item );
		}
		
		private function selectItem(event:MouseEvent):void {
			this.dispatchEvent( new ShopCarEvent ( ShopCarEvent.SELECT, {id: (event.target as ShopCarItem).id} ) );
		}
		
		public function step(value:int):void {
			if (slider)
				slider.step(value);
		}
		
	}
	
}